package cs341.group2.mycontacts;

import java.util.ArrayList;
import java.util.HashMap;

import cs341.group2.mycontacts.Database.MyContactsDB;

import android.content.Context;
import android.content.Intent;
import android.provider.SyncStateContract.Constants;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Filter;
import android.widget.ListView;
import android.widget.TextView;

public class ContactButtonAdapter extends BaseAdapter {
    private Context mContext;
    private MyContactsDB myContactsDb;
    private ArrayList<HashMap<String, String>> items;
    private ArrayList<HashMap<String, String>> originalItems;
    

    public ContactButtonAdapter(Context context, ArrayList<HashMap<String, String>> items) {
    	mContext = context;
    	myContactsDb = new MyContactsDB(mContext);
        this.items = items;
        this.originalItems = items;
    }
    
    // Declare button click listener variable
    private OnClickListener mOnButtonClick;

    // Method to set button click listener variable
    public void setOnButtonClickListener(OnClickListener listener) {
        mOnButtonClick = listener;
    }

    public ContactButtonAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return items.size();
    }

    public Object getItem(int position) {
        return items.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {
	   LayoutInflater inflater = LayoutInflater.from(parent.getContext());
	   view = inflater.inflate(R.layout.contacts_list, parent, false);
       
       final HashMap<String, String> item = items.get(position);
       
       final Button category = (Button)view.findViewById(R.id.contactName);
       category.setHeight(100);
       category.setText(item.get("contactName"));
       category.setId(Integer.parseInt(item.get("contactId")));
       
       //on click for remove from category button
       category.setOnClickListener(new View.OnClickListener() {
		
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(mContext, ContactPage.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				intent.putExtra("state", "view");
				intent.putExtra("contactId", Integer.parseInt(item.get("contactId")));
				mContext.startActivity(intent);
			}
		});
       
       return view;
       
    }
    
    public Filter getFilter() {
        return new Filter() {
            @SuppressWarnings("unchecked")
            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                //items = (ArrayList<HashMap<String, String>>) results.values;
                ContactButtonAdapter.this.notifyDataSetChanged();
            }

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
            	FilterResults results = new FilterResults();
            	ArrayList<HashMap<String, String>> temp = new ArrayList<HashMap<String, String>>();
            	if(constraint == null || constraint.length() == 0){
            		//no constraint, return full list
            		results.count = originalItems.size();
            		results.values = originalItems;
            		items = originalItems;
            	}
            	else{
            		constraint = constraint.toString().toLowerCase();
            		for(int i = 0; i <= originalItems.size()-1; i ++){
            			HashMap<String, String> map = originalItems.get(i);
            			String name = map.get("contactName").replaceAll("\\s","").toLowerCase();
            			String cons = constraint.toString().replaceAll("\\s","");
            			boolean add = name.startsWith(cons);
            			if(add == true)
            			{            		
            				temp.add(map);
            			}
            		}

                    results.count = temp.size();
                    results.values = temp;
                    items = temp;
            	}
                return results;
            }
        };
    }

}